package com.share51.admin.service.impl;

import com.share51.admin.dao.AdminMapper;
import com.share51.admin.dto.AdminDto;
import com.share51.admin.dto.EditAdminDto;
import com.share51.admin.entity.Admin;
import com.share51.admin.constant.AdminConstant;
import com.share51.admin.entity.Role;
import com.share51.admin.service.AdminService;
import com.share51.admin.util.PageUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Service("adminService")
public class AdminServiceImpl implements AdminService {

    @Resource
    private AdminMapper adminMapper;

    @Override
    public void addAdmin(AdminDto adminDto) {
        Admin admin = new Admin();
        BeanUtils.copyProperties(adminDto, admin);
        admin.setAddTime(new Date());
        adminMapper.insertSelective(admin);
    }

    @Override
    public void editAdmin(EditAdminDto adminDto) {
        Admin admin = new Admin();
        BeanUtils.copyProperties(adminDto, admin);
        admin.setUpdateTime(new Date());
        adminMapper.updateByPrimaryKeySelective(admin);
    }

    @Override
    public void deleteAdmin(int id) {
        Admin admin = new Admin();
        admin.setAdminId(id);
        admin.setStatus(AdminConstant.AdminStatus.DELETE.getCode());
        admin.setUpdateTime(new Date());
        adminMapper.updateByPrimaryKeySelective(admin);
    }

    @Override
    public void lockedAdmin(int id) {
        Admin admin = new Admin();
        admin.setAdminId(id);
        admin.setStatus(AdminConstant.AdminStatus.LOCKED.getCode());
        admin.setUpdateTime(new Date());
        adminMapper.updateByPrimaryKeySelective(admin);
    }

    @Override
    public void unLockedAdmin(int id) {
        Admin admin = new Admin();
        admin.setAdminId(id);
        admin.setStatus(AdminConstant.AdminStatus.NORMAL.getCode());
        admin.setUpdateTime(new Date());
        adminMapper.updateByPrimaryKeySelective(admin);
    }

    @Override
    public Admin getAdminById(int id) {
        return adminMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Admin> getAdminList(AdminDto adminDto, PageUtil pageUtil) {

        return null;
    }

    @Override
    public int getAdminTotal(AdminDto adminDto) {
        return 0;
    }

    @Override
    public void resetPassword(int id) {
        Admin admin = new Admin();
        admin.setAdminId(id);
        admin.setUpdateTime(new Date());
        admin.setPassword("");//TODO
        adminMapper.updateByPrimaryKeySelective(admin);
    }

    @Override
    public void editPassword(HttpServletRequest request, String oldPassword, String password, String rePassword) {
        //TODO
        if(StringUtils.isNotBlank(oldPassword)){

        }
        if(StringUtils.isNotBlank(password)){

        }
        if(StringUtils.isNotBlank(rePassword)){

        }
        Admin admin = new Admin();
        //admin.setAdminId(id); //TODO
        admin.setUpdateTime(new Date());
        admin.setPassword(password);
        adminMapper.updateByPrimaryKeySelective(admin);
    }

    @Override
    public List<Role> getRoleListByUser(int adminId) {
        return null;
    }
}
